package ye.guo.huang.java9;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.MultiResolutionImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HUANG GY.
 * @Date: 2021/11/10 11:23
 * @Description: ye.guo.huang.java9
 */
public class Test06Image {

    public static void main(String[] args) throws IOException {
        List<String> imgUrls = List.of("https://www.runoob.com/wp-content/themes/runoob/assets/img/runoob-logo@2x.png",
                "https://www.runoob.com/wp-content/themes/runoob/assets/img/runoob-logo.png",
                "https://www.runoob.com/wp-content/themes/runoob/assets/images/qrcode.png");

        Image [] images = new Image[3];
        for (int i = 0; i < imgUrls.size(); i++) {
            images[i]=ImageIO.read(new URL(imgUrls.get(i)));
        }

        // 读取所有图片
        MultiResolutionImage multiResolutionImage =
                new BaseMultiResolutionImage(images);

        // 获取图片的所有分辨率
        List<Image> variants = multiResolutionImage.getResolutionVariants();

        System.out.println("Total number of images: " + variants.size());

        for (Image img : variants) {
            System.out.println(img);
        }

        // 根据不同尺寸获取对应的图像分辨率
        Image variant1 = multiResolutionImage.getResolutionVariant(156, 45);
        System.out.printf("\nImage for destination[%d,%d]: [%d,%d]",
                156, 45, variant1.getWidth(null), variant1.getHeight(null));

        Image variant2 = multiResolutionImage.getResolutionVariant(311, 89);
        System.out.printf("\nImage for destination[%d,%d]: [%d,%d]", 311, 89,
                variant2.getWidth(null), variant2.getHeight(null));

        Image variant3 = multiResolutionImage.getResolutionVariant(622, 178);
        System.out.printf("\nImage for destination[%d,%d]: [%d,%d]", 622, 178,
                variant3.getWidth(null), variant3.getHeight(null));

        Image variant4 = multiResolutionImage.getResolutionVariant(300, 300);
        System.out.printf("\nImage for destination[%d,%d]: [%d,%d]", 300, 300,
                variant4.getWidth(null), variant4.getHeight(null));
    }
}